//
//  SetColorView.m
//  TheDragon
//
//  Created by wingfac on 2017/7/17.
//  Copyright © 2017年 LJ. All rights reserved.
//

#import "SetColorView.h"
@interface SetColorView()
@property(nonatomic,strong) UIView *bgView; //内容背景view


@end
@implementation SetColorView
{
    UIButton * selectButton;    // 记录选中按钮
    NSArray *colorArray;
    NSArray *colorStrArray;
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
        self.alpha = 0;
        UIView *backView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        backView.backgroundColor = [UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:0.75];
        [self addSubview:backView];
        
        UIColor *oneColor = getColor(whiteColor);
        UIColor *twoColor = getColor(mainColor);
        UIColor *threeColor = getColor(@"c26aed");
        UIColor *fourColor = getColor(@"b1c416");
        UIColor *fiveColor = getColor(@"585a52");
        
        colorArray = @[oneColor,twoColor,threeColor,fourColor,fiveColor];
        colorStrArray = @[@"ffffff",@"0aa948",@"c26aed",@"b1c416",@"585a52"];
        _bgView = [[UIView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT - 150/WIDTH_5S_SCALE, SCREEN_WIDTH, 150/WIDTH_5S_SCALE)];
        _bgView.backgroundColor = getColor(bgColor);
//        _bgView.layer.cornerRadius = 5;
        _bgView.layer.masksToBounds = YES;
        [backView addSubview:self.bgView];
        
        UILabel *systemLabel = [[UILabel alloc] initWithFrame:CGRectMake(15/WIDTH_5S_SCALE, 0, 60/WIDTH_5S_SCALE, 30/WIDTH_5S_SCALE)];
        systemLabel.text = @"设置";
        systemLabel.font = DEF_FontSize_12;
        [_bgView addSubview:systemLabel];
        
        UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(SCREEN_WIDTH - 60/WIDTH_5S_SCALE, 0, 60/WIDTH_5S_SCALE, 30/WIDTH_5S_SCALE)];
        button.titleLabel.font = DEF_FontSize_12;
        [button setTitle:@"完成" forState:UIControlStateNormal];
        [button setTitleColor:getColor(mainColor) forState:UIControlStateNormal];
        [button addTarget:self action:@selector(complete) forControlEvents:UIControlEventTouchUpInside];
        [_bgView addSubview:button];
        
        UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 30/WIDTH_5S_SCALE, SCREEN_WIDTH, 1/WIDTH_5S_SCALE)];
        line.backgroundColor = getColor(dividerColor);
        [_bgView addSubview:line];
        
        UILabel *_titleLabel  = [[UILabel alloc]initWithFrame:CGRectMake(15/WIDTH_5S_SCALE, 32/WIDTH_5S_SCALE, 80/WIDTH_5S_SCALE, 30/WIDTH_5S_SCALE)];
        _titleLabel.text = @"主题颜色";
        _titleLabel.font = DEF_FontSize_12;
        [_bgView addSubview:_titleLabel];
        
        
        for(int i=0;i<5;i++){
            UIButton *colorBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            colorBtn.frame = CGRectMake(50/WIDTH_5S_SCALE + 60*i, 70/WIDTH_5S_SCALE, 25/WIDTH_5S_SCALE, 25/WIDTH_5S_SCALE);
            colorBtn.layer.masksToBounds = YES;
            colorBtn.layer.cornerRadius = 5;
            [colorBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [colorBtn setBackgroundColor:colorArray[i]];
            colorBtn.tag = 1000+i;
            [colorBtn addTarget:self action:@selector(chooseColor:) forControlEvents:UIControlEventTouchUpInside];
            [_bgView addSubview:colorBtn];
        }
        

        

    }
    return self;
}
- (void)chooseColor:(UIButton *)button{
    [selectButton setImage:nil forState:UIControlStateNormal];
    switch (button.tag) {
        case 1000:
        {
            selectButton = button;
        }
            break;
        case 1001:
        {
            selectButton = button;

        }
            break;
        case 1002:
        {
            selectButton = button;

        }
            break;
        case 1003:
        {
            selectButton = button;

        }
            break;
        case 1004:
        {
            selectButton = button;

        }
            break;
            
        default:
            break;
    }
    [selectButton setImage:[UIImage imageNamed:@"selected"] forState:UIControlStateNormal];

    
}

- (void)complete{
    if(selectButton.tag){
        [self.delegate selectedColor:colorArray[selectButton.tag -1000] colorString:colorStrArray[selectButton.tag -1000]];

    }
    [self hide];
}

#pragma mark -- show
//出现
-(void)show{
    [[UIApplication sharedApplication].delegate.window addSubview:self];
    [UIView animateWithDuration:.15 animations:^{
        self.alpha = 1;
    }];
}
- (void)hide{
    [UIView animateWithDuration:.15 animations:^{
        self.alpha = 0 ;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}
@end
